﻿using BathhouseService.Models.Commonality;
using BathhouseService.Models.SqlModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;

namespace BathhouseService.Areas.CURD.ViewModels.ReportFormVMs
{
    public class ServiceReportFormListVM: BasePagedListVM<ServiceReportForm_View, ServiceReportSearcher>
    {
        protected override List<GridAction> InitGridAction()
        {
            return new List<GridAction>
            {

                //this.MakeAction(null,null,"服务详情",null, GridActionParameterTypesEnum.SingleId).SetOnClickScript("test").SetShowInRow(true).SetHideOnToolBar(true),             
            };
        }
        protected override IEnumerable<IGridColumn<ServiceReportForm_View>> InitGridHeader()
        {
            return new List<GridColumn<ServiceReportForm_View>>{
                this.MakeGridHeader(x => x.Name).SetFormat((a,b)=>{
                 string re = UIService.MakeDialogButton(ButtonTypesEnum.Link,$"/CURD/ReportForm/Details?ID={a.ID}&date={a.CurrentMonth}",a.Name ,800,null);
                //string re = $"<a href='/CURD/ReportForm/Details?ID={a.ID}&date={a.CurrentMonth}' target='_blank'>{a.Name }</a>";
                    return re;
                }),
                this.MakeGridHeader(x => x.Count),
                this.MakeGridHeader(x => x.CurrentMonth),
                this.MakeGridHeaderAction(width: 200)

            };
        }
        public override IOrderedQueryable<ServiceReportForm_View> GetSearchQuery()
        {

            var currentMonth = string.Empty;

            var date = DateTime.Now;
            var startMonth= DateCalculation.StartMonth(date);
            var endMonth= DateCalculation.EndMonth(date);
            var query = from s in DC.Set<ServiceRecord>() select s;            
            if (!string.IsNullOrWhiteSpace(Searcher.ServicePersonName))
            {
                query = query.CheckContain(Searcher.ServicePersonName, x => x.ServicePersonName);
            }
            if (Searcher.PaymentTime!=null)
            {
                query = query.CheckBetween(Searcher.PaymentTime?.GetStartTime(), Searcher.PaymentTime?.GetEndTime(), x => x.PaymentTime, includeMax: false);
                currentMonth = Convert.ToDateTime(Searcher.PaymentTime.GetStartTime()).ToString("yyyy-MM");
            }
            else if(Searcher.PaymentTime == null)
            {
                query = query.Where(x => x.PaymentTime > startMonth && x.PaymentTime < endMonth);
                currentMonth = startMonth.ToString("yyyy-MM");
            }
            var list = query
            .GroupBy(x=>x.ServicePersonId)
                .Select(x => new ServiceReportForm_View
                {
                   ID = x.Key,
                   Name = DC.Set<FrameworkUserBase>().SingleOrDefault(y=>y.ID==x.Key).Name,
                   Count = x.Count(),
                   CurrentMonth = currentMonth
                })
                .OrderByDescending(x => x.Count);
            return list;
        }
    }

    public class ServiceReportForm_View:TopBasePoco 
    {
        [Display(Name = "师傅")]
        public string Name { get; set; }
        [Display(Name = "服务总次数")]

        public int Count { get; set; }
        [Display(Name = "时间")]
        public string CurrentMonth { get; set; }
    }
}
